package com.xudow.app.newui;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bokecc.sdk.mobile.live.DWLive;
import com.bokecc.sdk.mobile.live.DWLiveLoginListener;
import com.bokecc.sdk.mobile.live.Exception.DWLiveException;
import com.bokecc.sdk.mobile.live.pojo.RoomInfo;
import com.bokecc.sdk.mobile.live.pojo.TemplateInfo;
import com.bokecc.sdk.mobile.live.pojo.Viewer;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplay;
import com.bokecc.sdk.mobile.live.replay.DWLiveReplayLoginListener;
import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.xudow.app.Config;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.directseeding.LiveReplayActivity;
import com.xudow.app.directseeding.LiveRoomActivity;
import com.xudow.app.directseeding.entity.LiveRoom;
import com.xudow.app.network.AjaxCallBack;
import com.xudow.app.network.OkHttpClientManager;
import com.xudow.app.newui.adapter.DirectseedingAdapter;
import com.xudow.app.newui.task.LiveListTask;
import com.xudow.app.newui.zutils.LsUtils;
import com.xudow.app.ui.task.CourseListTask;
import com.xudow.app.ui.widget.LoadingDialog;
import com.xudow.app.util.Maps;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * 作者：LownChen on 2016/9/22 16:01
 */

public class DirectseedingActivity extends Activity implements  View.OnClickListener{

   SwipeRefreshLayout svView;
   ListView listView;


    ImageView back;
     RelativeLayout meishuju;
    List<LiveRoom.ScreenEntity>  lrss=new ArrayList<LiveRoom.ScreenEntity>();
    boolean isFenYe=true;
    /**
     * 获取课程Handler
     */
    private Handler courseListHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
//            LsUtils.pdDismiss();
            hideLoadingDialog();
            if (msg.what == CourseListTask.SUCCESS) {


               Bundle data=msg.getData();

               List<LiveRoom.ScreenEntity> lrs= (List<LiveRoom.ScreenEntity>) data.getSerializable("courses");

                Log.i("DirectseedingActivity","lrs.size()>>>"+lrs.size());
                if(lrs.size()==15){
                    isFenYe=true;
                }else{
                    isFenYe=false;
                }
                for(LiveRoom.ScreenEntity le:lrs){
                    lrss.add(le);
                }


         Log.i("DirectseedingActivity","lrss.size()>>>"+lrss.size());
                adapter=new DirectseedingAdapter(DirectseedingActivity.this,lrss);
                    listView.setAdapter(adapter);
            }
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_directseeding);

        initView();

        initData();
    }
    int page=1;
    int offset=15;
    DirectseedingAdapter adapter;
    LiveListTask liveListTask;
    private void initData() {

        showLodingDialog("正在加载...");
        HashMap<String, String> params = Maps.newHashMap();
        params.put("page",page+"");
        params.put("offset","15");
       String url=Config.BASE_URL + "app/live/broadcastLivecc";
        liveListTask=new LiveListTask(this,courseListHandler,url);
        liveListTask.execute(params);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (view.getLastVisiblePosition() == (totalItemCount - 1)) {    //判断是否滑动到最底部
                    if (isFenYe) {
                        Log.i("DirectseedingActivity", "isFenYe>>>" + isFenYe);
                       page++;
                       HashMap<String, String> params = Maps.newHashMap();
                       params.put("page", page + "");
                       Log.i("DirectseedingActivity", "page>>>" + page);
                       Log.i("liveactivityt", "");
                       params.put("offset","15");
                       String url=Config.BASE_URL + "app/live/broadcastLivecc";
                       liveListTask=new LiveListTask(DirectseedingActivity.this,courseListHandler,url);
                       liveListTask.execute(params);

                    }
                }
            }
        });


    }



    private void initView() {
      View  loadView = LayoutInflater.from(this).inflate(R.layout.load_data, null);
        back=(ImageView)findViewById(R.id.back);
        back.setOnClickListener(this);
        svView=(SwipeRefreshLayout)findViewById(R.id.sv_view);
        listView=(ListView)findViewById(R.id.lv);


        meishuju=(RelativeLayout)findViewById(R.id.meishuju);

        svView.setColorSchemeResources(
                android.R.color.holo_blue_light,
                android.R.color.holo_green_light,
                android.R.color.holo_green_light,
                android.R.color.holo_green_light
        );
        svView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                initData();

                svView.setRefreshing(false);

            }
        });


//        xrView.addItemDecoration(new SpaceItemDecoration(R.drawable.dr_item));
//        xrView.addItemDecoration(new RecycleViewDivider(
//                this, LinearLayoutManager.VERTICAL, R.drawable.dr_item));
//        svView.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_green_light, android.R.color.holo_green_light, android.R.color.holo_green_light);
//      xrView.addItemDecoration(new SpaceItemDecoration(DisplayUtils.dip2px(this, 15)));



        }


    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.back:

                this.finish();

              break;
      }
    }
      String desc;
      String imagpath,name;

    LoadingDialog _loadingDialog;
    /**
     * 显示加载对话框
     *
     * @param message
     */
    public void showLodingDialog(String message) {
        if (null != _loadingDialog) {          return;
        }
        _loadingDialog = new LoadingDialog(this, message);
        _loadingDialog.setCancelable(true);

        _loadingDialog.show();
    }

    /**
     * 隐藏登录对话框
     */
    public void hideLoadingDialog() {
        if (null != _loadingDialog && _loadingDialog.isShowing()) {
            _loadingDialog.dismiss();
            _loadingDialog = null;
        }
    }

}